{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Pmod PWM\n",
    "\n",
    "In this notebook, The Pmod PWM driver is exercised.  Specifically, An AXI Timer is used to a generate pulse width modulated (PWM) signal.  \n",
    "\n",
    "To see the results of this notebook, you will need a [Digilent Analog Discovery 2](http://store.digilentinc.com/analog-discovery-2-100msps-usb-oscilloscope-logic-analyzer-and-variable-power-supply/)\n",
    "\n",
    "<td> <img src=\"http://cdn6.bigcommerce.com/s-7gavg/products/468/images/2617/Analog_Discovery_2_obl_Academic_600__01249.1447804398.1280.1280.png\" alt=\"Drawing\" style=\"width: 250px;\"/> </td>\n",
    "\n",
    "and [WaveForms 2015](https://reference.digilentinc.com/waveforms3#newest)\n",
    "\n",
    "<td> <img src=\"https://reference.digilentinc.com/_media/reference/software/waveforms/waveforms-3/waveforms3-0.png\" alt=\"Drawing\" style=\"width: 250px;\"/> </td>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Instantiation\n",
    "Import overlay and instantiate Pmod_PWM class. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from pynq.overlays.base import BaseOverlay\n",
    "base = BaseOverlay(\"base.bit\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Connect Scope\n",
    "In this example, we choose the Digilent Analog Discovery 2 as the scope. \n",
    "\n",
    "* The `1+` pin (of channel 1) has to be connected to pin 0 on PMODA interface. \n",
    "* The `1-` pin (of channel 1) has to be connected to `GND` on PMODA interface. \n",
    "\n",
    "This example uses PMODA interface. In order to use PMODB interface, users can replace PMODA to PMODB in the examples below. Similarly, users can change the pin number."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "from pynq.lib import Pmod_PWM\n",
    "\n",
    "pwm = Pmod_PWM(base.PMODA,0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Generate a clock of $50\\%$ duty cycle and $10\\,\\mu$s period\n",
    "\n",
    "In this example, we generate a $10\\,\\mu$s clocks with $50\\%$ duty cycle for 4 seconds and the stop. Issuing stop command stops both timer sub-modules.\n",
    "\n",
    "Users have to choose channel 1 for waveform display in the scope.\n",
    "\n",
    "The output would look like this:\n",
    "\n",
    "<img src=\"data/pwm_50_duty_cycle.jpg\" width=\"791px\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# Generate a 10 us clocks with 50% duty cycle\n",
    "period=10\n",
    "duty=50\n",
    "pwm.generate(period,duty)\n",
    "\n",
    "# Sleep for 4 seconds and stop the timer\n",
    "time.sleep(4)\n",
    "pwm.stop()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Generate a clock of $25\\%$ duty cycle and $20\\,\\mu$s period\n",
    "\n",
    "Repeating the above test for another set of parameters. The output would look like this:\n",
    "\n",
    "<img src=\"data/pwm_25_duty_cycle.jpg\" width=\"791px\"/>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "\n",
    "# Generate a 20 us clocks with 25% duty cycle\n",
    "period=20\n",
    "duty=25\n",
    "pwm.generate(period,duty)\n",
    "\n",
    "# Sleep for 5 seconds and stop the timer\n",
    "time.sleep(5)\n",
    "pwm.stop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
